package com.hzhr.iot.web.controller;

import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.user.sdk.model.company_name.Company_NameModel;
import com.hzhr.iot.user.sdk.service.User_Company_NameService;
import com.hzhr.iot.web.Global;
import com.hzhr.iot.web.constant.UserOptions;
import com.hzhr.iot.web.controller.base.BaseController;
import com.hzhr.iot.web.model.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/company")
@Api(description = "公司")
@RestController
public class CompanyController extends BaseController {

    @Autowired
    User_Company_NameService user_company_nameService;
    @Autowired
    Global global;
    @Autowired
    UserOptions userOptions;


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiOperation(value = "获取公司列表", notes = "获取公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "单页面条数", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "公司类型, 0 所有公司 1 燃气公司 2 物联网设备公司", dataType = "String", required = false, paramType = "query")
    })
    @RequestMapping(value = "/getcompanylist", method = RequestMethod.POST)
    BaseResponse getCompanyList(@RequestParam(value = "type", required = false,defaultValue = "0") int type
            , @RequestParam(value = "uat", required = true) String uat
            , @RequestParam(value ="pageNum", required = false,  defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", required = false, defaultValue = "50") int pageSize) throws Exception {
        Map<String, String> header = new HashMap<>();
        header.put("token", global.getFirstToken());
        Map<String, Object> query = new HashMap<>();
        if (type == 1) {
            query.put("_equal_type", userOptions.getCode("COMPANY_TYPE", "GAS_COMPANY"));
        } else if (type == 2) {
            query.put("_equal_type", userOptions.getCode("COMPANY_TYPE", "IOT_DEVICE_COMPANY"));
        }
        query.put("_show", "list");
        query.put("_show_info", true);

        Map<String, Object> result = new HashMap<>();
        List<Map<String, String>> dataList = new ArrayList<>();
        result.put("dataList", dataList);
        List<Company_NameModel> companyModels = user_company_nameService.get(header, query).getModelList();
        for (Company_NameModel companyModel :
                companyModels) {
            Map<String, String> dataItem = new HashMap<>();
            dataItem.put("id", HzhrTypeUtil.integer2string(companyModel.getId()));
            dataItem.put("name", companyModel.getName());
            dataList.add(dataItem);
        }

        result.put("pageNum",pageNum);
        result.put("pageSize",pageSize);
        return new BaseResponse("0", "成功",result );
    }
}
